package com.wmcom.wm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wmcom.wm.dto.DishDto;
import com.wmcom.wm.entity.Dish;
import com.wmcom.wm.entity.DishFlavor;
import com.wmcom.wm.mapper.DishMapper;
import com.wmcom.wm.service.DishFlavorService;
import com.wmcom.wm.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Resource
   private DishFlavorService dishFlavorService;
    /**
     * 新增菜品，同时插入菜品对应的口味数据，需要操作俩张表Dish，DishFlavor
     */
    @Override
    public void saveWithDishFlavor(DishDto dishDish) {
          //保存菜品的信息
       this.save(dishDish);
        //菜品id
        Long id = dishDish.getId();
    //    菜品口味
        List<DishFlavor> flavors = dishDish.getFlavors();
         flavors.forEach(dishDish1 -> {
              dishDish1.setDishId(id);
         });
        // 保存菜品口味数据到DishFlavor表中
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id查询菜品信息和口味信息，需要操作俩张表Dish，DishFlavor
     */
    @Override
    public DishDto getDishDto(Long id) {
        //查询菜品信息
        Dish dish = this.getById(id);
        //拷贝数据
        DishDto dishDto=new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        //查询菜品口味
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> list = dishFlavorService.list(wrapper);
        dishDto.setFlavors(list);
        return dishDto;
    }
}
